package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallReportMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @program: mall
 * @author: youvy
 * @create: 2019-12-24 11:03
 **/
@Service
public class LitemallReportService {

	@Resource
	private LitemallReportMapper reportMapper;

	public List<LitemallReport> selectByCondition(int page, int pageSize, String type, String status, String brandId,String sort) {
		LitemallReportExample example = new LitemallReportExample();
		LitemallReportExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(type)) {
			criteria.andTypeEqualTo(Integer.parseInt(type));
		}

		if (StringUtils.isNotEmpty(status)) {
			criteria.andStatusEqualTo(Integer.parseInt(status));
		}

		if (StringUtils.isNotEmpty(brandId)) {
			criteria.andBrandIdEqualTo(Integer.parseInt(brandId));
		}


		if (sort != null && sort.length() > 0) {
			example.setOrderByClause(sort);
		}
		PageHelper.startPage(page, pageSize);
		return reportMapper.selectByExample(example);
	}

	public int update(LitemallReport report) {
		return reportMapper.updateByPrimaryKeySelective(report);
	}

	public int insert(LitemallReport report) {
		return reportMapper.insertSelective(report);
	}

	public long selectByUIdAndBrandId(int userId, Integer brandId) {
		LitemallReportExample example = new LitemallReportExample();
		example.or().andBrandIdEqualTo(brandId).andUserIdEqualTo(userId);
		return reportMapper.countByExample(example);
	}
}
